const Base = require('./base.js');

module.exports = class extends Base {
  async bannerAction() {
    const data = await this.model('banner').order(['id DESC']).select();
    // const sum = await this.model('domain_list').where({ status: ['NOTIN', '11,12,31,32,33'] }).sum('price_back');
    return this.success({data: data});
  }
  async questionAction() {
    const type = this.get('type') || 1;
    const data = await this.model('question').order(['id DESC']).where({type: type}).page(1, 30).countSelect();
    return this.success(data);
  }
  async newsListAction() {
    const page = this.get('page') || 1;
    const size = this.get('size') || 10;
    const data = await this.model('activity').order(['id DESC']).page(page, size).countSelect();
    return this.success(data);
  }
  async anliListAction() {
    const data = await this.model('anli').order(['id DESC']).select();
    return this.success(data);
  }
  async domainListAction() {
    const query = this.post();
    const page = this.post('page') || 1;
    const size = this.post('size') || 10;
    delete query.page;
    delete query.size;

    query.type = ['<', 2];

    // if (query.searchKey) {}
    if (query.searchKey && Object.keys(query.searchKey).length !== 0) {
      const search = [];
      for (const key in query.searchKey) {
        if (Object.hasOwnProperty.call(query.searchKey, key)) {
          const d = this.model('filter').find(key, query.searchKey[key]);
          if (d.w === 'like') {
            query[d.key] = [d.w, `%${d.value}%`];
          } else if (d.w === 'regex') {
            search.push(`%${d.label}%`);
          } else {
            query[d.key] = [d.w, d.value];
          }
        }
      }
      if (search.length) {
        query.search = ['like', search];
      }
    }
    delete query.searchKey;

    // if (query.order_money) {
    //   const arr = query.order_money.split('-');
    //   query.price = ['between', arr];
    //   delete query.order_money;
    // }

    // if (query.order_time) {
    //   const arr = query.order_time.split('-');
    //   query.days = ['between', arr];
    //   delete query.order_time;
    // }

    // if (query.domain_tag) {
    //   query.domain = ['like', ''];
    //   delete query.domain_tag;
    // }

    query.status = ['NOTIN', '11,12,31,32,33,51,52,53,61,62'];

    const data = await this.model('domain_list').where(query).order(['jiaoyi ASC', 'recommend DESC', 'id DESC']).page(page, size).countSelect();

    for (const item of data.data) {
      let targets = [];
      if (item.target) {
        targets = item.target.split(',');
      }
      if (item.hide === 1 && item.user_id != this.getLoginUserId() && !targets.includes('' + this.getLoginUserId())) {
        const str = item.domain.split(',')[0];
        const arr = str.split('.');
        item.domainMain = arr[0].slice(0, 1) + '****.' + arr[1];
      } else {
        item.domainMain = item.domain.split(',')[0];
      }
      item.domainNum = item.domain.split(',').length;
      item.domainList = item.domain.split(',');
      const s = this.model('status').find(item.status);
      item.statusTxt = s[0].label;
    }
    return this.success(data);
  }
  async domainIndexAction() {
    const query = {
      status: ['NOTIN', '11,12,31,32,33,51,52,53,61,62'],
      type: ['<', 2]
    };

    const data = await this.model('domain_list').where(query).order(['jiaoyi ASC', 'recommend DESC', 'id DESC']).page(1, 10).countSelect();
    for (const item of data.data) {
      let targets = [];
      if (item.target) {
        targets = item.target.split(',');
      }
      if (item.hide === 1 && item.user_id != this.getLoginUserId() && !targets.includes('' + this.getLoginUserId())) {
        const str = item.domain.split(',')[0];
        const arr = str.split('.');
        item.domainMain = arr[0].slice(0, 1) + '****.' + arr[1];
      } else {
        item.domainMain = item.domain.split(',')[0];
      }
      item.domainNum = item.domain.split(',').length;
      item.domainList = item.domain.split(',');
      const s = this.model('status').find(item.status);
      item.statusTxt = s[0].label;
    }
    return this.success(data);
  }
  async getTxtAction() {
    const data = await this.model('set').where({id: this.get('id')}).find();
    return this.success(data);
  }
  async getActivityAction() {
    const model = this.model('activity');
    const data = await model.where({id: this.get('id')}).find();
    return this.success(data);
  }

  async getFilterAction() {
    const s = this.model('filter').list();
    return this.success(s);
  }

  async getDictAction() {
    const model = this.model('dict_item');
    const data = await model.order(['sort ASC']).where({type: this.get('type')}).select();
    return this.success(data);
  }
  async domainAllAction() {
    const data = await this.model('domain_my').order(['id DESC']).limit(25).getField('domain', false);
    return this.success(data);
  }

  async ttttAction() {
    // const data = await this.model('activity').where({ActivityDiscription: ['!=', '']}).field('ActivityDiscription, ActivityId').select();
    // for (const item of data) {
    //   // http://gongqingtuanimg.hcbxsoft.cn
    //   const arr1 = item.ActivityDiscription.split('http://gongqingtuanimg.hcbxsoft.cn');
    //   item.ActivityDiscription = arr1.join('http://wximg.youngcommunist.com');

    //   const arr2 = item.ActivityDiscription.split('http://gongqingtuanadmin.hcbxsoft.cn');
    //   item.ActivityDiscription = arr2.join('http://wxadmin.youngcommunist.com');
    //   // console.log(111)
    //   // https://com-hcbx-app-img.oss-cn-beijing.aliyuncs.com
    //   const arr3 = item.ActivityDiscription.split('https://com-hcbx-app-img.oss-cn-beijing.aliyuncs.com');
    //   item.ActivityDiscription = arr3.join('https://qingchun666.oss-cn-beijing.aliyuncs.com');
    //   await this.model('activity').where({ActivityId: item.ActivityId}).update({
    //     ActivityDiscription: item.ActivityDiscription
    //   });
    // }
    return this.success();
  }
};
